package com.shop.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.DtsAccountTraceMapper;
import com.shop.pojo.DtsAccountTrace;
import com.shop.pojo.DtsUserAccount;
import com.shop.service.DtsAccountTraceService;
import com.shop.service.DtsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DtsAccountTraceServiceImpl
 * @Author xiao宋
 * @Date 18:14  2022/10/15
 * @Version 1.0
 * @Description TODO
 */
@Service
public class DtsAccountTraceServiceImpl implements DtsAccountTraceService {
    @Autowired
    DtsUserService userService;
    @Autowired
    DtsAccountTraceMapper dtsAccountTraceMapper;

    @Override
    public Map<String, Object> findByPage(
            Integer page, Integer limit, String sort,
            String order, String username, String mobile,
            List<Integer[]> statusArray
    ) {
        IPage<DtsAccountTrace> iPage = new Page<>(page, limit);
        QueryWrapper w = new QueryWrapper<>();


        if (!StringUtils.isEmpty(mobile)) {
            w.eq("mobile", mobile);
        }
        if (statusArray.size() > 0) {
            for (Integer[] i : statusArray) {
//                String s = String.valueOf(i);
                w.eq("status", statusArray.get(i[0]));
            }
        }
        if (!StringUtils.isEmpty(username)) {
            Integer user_id=userService.findByName(username);
            w.eq("user_id", user_id);
        }
//        排序方式
        if ("desc".equalsIgnoreCase(order)) {
            w.orderByDesc(sort);
        } else {
            w.orderByAsc(sort);
        }

        dtsAccountTraceMapper.selectPage(iPage, w);
//      封装数据
        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("traceList", iPage.getRecords());

        return data;
    }

    @Override
    public boolean approve(DtsAccountTrace account) {
        return dtsAccountTraceMapper.updateById(account) > 0;
    }
}
